"""
@author: 江同学呀
@file: aggregate_metric_double_field.py
@date: 2024/7/27 21:21
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/aggregate-metric-double.html

    Pre-aggregated metric values.
    预先聚合的指标值。
"""
from abc import ABC
from typing import Type, Union, Dict, List

from espc.common.field_common import TimeSeriesMetric, Metrics
from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_field import _BaseField
from espc.orm.model.mapping.field.field_attribute import TimeSeriesMetricAttribute, MetricsAttribute, DefaultMetricsAttribute
from espc.utils.build_add_attr import build_add_attr


class AggregateMetricDoubleField(
    _BaseField, MetricsAttribute, DefaultMetricsAttribute, TimeSeriesMetricAttribute, ABC
):
    """
    聚合度量 字段
    """
    _field_type: str = "aggregate_metric_double"

    def __init__(
            self, field_name: str = None, value: Dict[str, float] = None, default: Dict[str, float] = None,
            metrics: List[Union[str, Metrics]] = None,  default_metric: Union[str, Metrics] = None,
            time_series_metric: Union[str, TimeSeriesMetric] = None
    ):
        super().__init__(
            field_name=field_name, value=value, default=default, metrics=metrics, default_metric=default_metric,
            time_series_metric=time_series_metric
        )
        return

    def __get__(
            self, instance: _Base, owner: Type["AggregateMetricDoubleField"]
    ) -> Union["AggregateMetricDoubleField", Dict[str, float]]:
        return super(AggregateMetricDoubleField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: Dict[str, float]):
        super(AggregateMetricDoubleField, self).__set__(instance=instance, value=value)
        return

    @property
    def value(self) -> Dict[str, float]:
        return self._value

    @value.setter
    def value(self, value: Dict[str, float]):
        self._value: Dict[str, float] = value
        return

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping


